package com.wanban.adutils.tools

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import com.google.android.gms.ads.nativead.NativeAd
import com.google.android.gms.ads.nativead.NativeAdView
import com.wanban.adutils.R
import com.wanban.adutils.callback.OnLoadAdCallback

object AdmobViewTools {

    fun showOpenAd(adRoot: ViewGroup, nativeAd: NativeAd) {
        val adViewContainer = LayoutInflater.from(adRoot.context.applicationContext).inflate(
            R.layout.admob_open,
            adRoot, false
        )
        val adView = adViewContainer.findViewById<NativeAdView>(R.id.adView)
        adView?.headlineView = adView.findViewById(R.id.ad_headline)
        adView?.bodyView = adView.findViewById(R.id.ad_body)
        adView?.mediaView = adView.findViewById(R.id.ad_media)
        adView?.iconView = adView.findViewById(R.id.ad_logo)
        adView?.adChoicesView = adView.findViewById(R.id.ad_choices_container)
        adView?.callToActionView = adView.findViewById(R.id.ad_call_to_action)

        (adView?.headlineView as TextView?)?.text = nativeAd.headline

        if (nativeAd.body == null) {
            adView?.bodyView?.visibility = View.INVISIBLE
        } else {
            adView?.bodyView?.visibility = View.VISIBLE
            (adView?.bodyView as TextView?)?.text = nativeAd.body
        }

        if (nativeAd.icon == null) {
            adView?.iconView?.visibility = View.GONE
        } else {
            adView?.iconView?.visibility = View.VISIBLE
            (adView?.iconView as ImageView?)?.setImageDrawable(nativeAd.icon!!.drawable)
        }

        if (nativeAd.callToAction == null) {
            adView?.callToActionView?.visibility = View.GONE
        } else {
            adView?.callToActionView?.visibility = View.VISIBLE
            (adView?.callToActionView as TextView?)?.text = nativeAd.callToAction
        }

        adView.setNativeAd(nativeAd)
        if (adRoot.visibility != View.VISIBLE) {
            adRoot.visibility = View.VISIBLE
        }
        adRoot.addView(adViewContainer)
    }

    fun showInstreamAd(adRoot: ViewGroup, nativeAd: NativeAd) {
        val adViewContainer = LayoutInflater.from(adRoot.context.applicationContext).inflate(
                R.layout.admob_instream,
                adRoot, false
        )
        val adView = adViewContainer.findViewById<NativeAdView>(R.id.adView)
        adView?.headlineView = adView.findViewById(R.id.ad_headline)
        adView?.bodyView = adView.findViewById(R.id.ad_body)
        adView?.mediaView = adView.findViewById(R.id.ad_media)

        (adView?.headlineView as TextView?)?.text = nativeAd.headline

        if (nativeAd.body == null) {
            adView?.bodyView?.visibility = View.INVISIBLE
        } else {
            adView?.bodyView?.visibility = View.VISIBLE
            (adView?.bodyView as TextView?)?.text = nativeAd.body
        }

        adView.setNativeAd(nativeAd)
        if (adRoot.visibility != View.VISIBLE) {
            adRoot.visibility = View.VISIBLE
        }
        adRoot.addView(adViewContainer)
    }


    fun showPauseVideoAd(adRoot: ViewGroup, nativeAd: NativeAd, listener: OnLoadAdCallback?): NativeAdView {
        val adViewRoot = LayoutInflater.from(adRoot.context.applicationContext).inflate(
            R.layout.admob_pause,
            adRoot, true
        )
        val adView = adViewRoot.findViewById<NativeAdView?>(R.id.adView)
        adView?.headlineView = adView.findViewById(R.id.ad_headline)
        adView?.bodyView = adView.findViewById(R.id.ad_body)
        adView?.mediaView = adView.findViewById(R.id.ad_media)

        (adView.headlineView as TextView).text = nativeAd.headline

        if (nativeAd.body == null) {
            adView?.bodyView?.visibility = View.INVISIBLE
        } else {
            adView?.bodyView?.visibility = View.VISIBLE
            (adView?.bodyView as TextView?)?.text = nativeAd.body
        }

        adView.setNativeAd(nativeAd)
        if (adRoot.visibility != View.VISIBLE) {
            adRoot.visibility = View.VISIBLE
        }
        val layoutClose = adViewRoot.findViewById<View>(R.id.layoutClose)
        if (layoutClose != null && !layoutClose.hasOnClickListeners()) {
            layoutClose.setOnClickListener {
                listener?.onAdComplete()
            }
        }
        return adView
    }

}